#include "stdafx.h"
#include "Dialog.h"

Dialog::Dialog(HINSTANCE instance,LPCTSTR templateName,
	Window *parentWindow){
	inst=instance;
	create(templateName,parentWindow);
}

void Dialog::checkButton(int id,uint check){
	CheckDlgButton(hwnd,id,check);
}
PTSTR Dialog::className(void){
	return L"DIALOG";
}
void Dialog::create(LPCTSTR templateName,Window *parentWindow){
	hwnd=CreateDialogParam(inst,templateName,parentWindow?parentWindow->hwnd:0,
		dialogProc,(LPARAM)this);
	SetWindowLong(hwnd,DWL_USER,(LONG)this);
}
BOOL CALLBACK Dialog::dialogProc(HWND dialog,UINT message,WPARAM wparam,
	LPARAM lparam){
	Dialog *ptr=(Dialog*)GetWindowLong(dialog,DWL_USER);
	switch(message){
	case WM_COMMAND:
		return ptr->onCommand(wparam,lparam);
	case WM_DEVICECHANGE:
		return ptr->onDeviceChange(wparam,lparam);
	case WM_INITDIALOG:
		SetWindowLong(dialog,DWL_USER,lparam);
		((Dialog*)lparam)->hwnd=dialog;
		return((Dialog*)lparam)->onInitDialog(lparam);
	case WM_SETCURSOR:
		ptr->onSetCursor();
		break;
	default:
		return 0;
	}
	return 1;
}
void Dialog::end(INT_PTR result){
	EndDialog(hwnd,0);
}
uint Dialog::isButtonChecked(int id){
	return IsDlgButtonChecked(hwnd,id);
}
BOOL Dialog::messageLoop(void){
	BOOL res;
	MSG msg;
	while(res=GetMessage(&msg,NULL,0,0)){
		if(res==-1){
			// Handle the error and possibly exit
		}else if(!IsWindow(hwnd)||!IsDialogMessage(hwnd,&msg)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} 
	} 
	return res;
}
LRESULT Dialog::onCommand(WPARAM wparam,LPARAM lparam){
	if (LOWORD(wparam)==IDOK||LOWORD(wparam)==IDCANCEL){
		end(LOWORD(wparam));
		return TRUE;
	}
	return FALSE;
}
BOOL Dialog::onInitDialog(LPARAM lparam){
	return TRUE;
}
void Dialog::onSetCursor(void){
}
BOOL Dialog::onCharToItem(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCompareItem(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorBtn(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorDlg(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorEdit(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorListbox(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorScrollbar(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onCtlColorStatic(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onQueryDragIcon(WPARAM wparam,LPARAM lparam){
	return 0;
}
BOOL Dialog::onVKeyToItem(WPARAM wparam,LPARAM lparam){
	return 0;
}